package org.algorithm.Thread;

public class ThreadSolution {

    public static void main(String[] args) {

        for (int i = 0; i < 3; i++) {
            new Thread(new Print(i)).start();
        }
    }


    static int end = 100;
    static int start = 0;

    static Object lock = new Object();

    static class Print implements Runnable {

        int index;

        public Print(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            while (start <= end) {
                synchronized (lock) {
                    while (start % 3 != index) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    if (start<=end){
                        System.out.println(Thread.currentThread().getId()+"---->"+start);
                        start++;
                        lock.notifyAll();
                    }

                }

            }
        }
    }


}
